/**
 *
 */

import { isArray } from 'fc-cli-core'
import { COLUMN_TYPE } from './FCRuleTableConstant'

class FCRuleTableColumn {
  /* properties */

  title
  type
  valuePath = []
  children = []
  parentDataIndex
  isMerge

  dataIndex

  /* getter */

  get hasChildren() {
    return isArray(this.children) && this.children.length > 0
  }

  // addSubColumn(subColumn) {
  //   this.children.push(subColumn)
  // }

  /* lifecycle methods */

  constructor({ title, type = COLUMN_TYPE.colCondition, dataIndex, valuePath = [], parentDataIndex, children } = {}) {
    this.title = title
    this.type = type
    this.dataIndex = dataIndex
    this.valuePath = valuePath
    this.children = children
    this.parentDataIndex = parentDataIndex
  }

  /* public methods */

  buildTableColumn() {
    if (this.hasChildren) {
      const children = this.children.map((column) => column.buildTableColumn())
      return { title: this.title, children }
    }

    return this.buildTableColumnData()
  }

  buildTableColumnData() {
    return {
      title: this.title,
      dataIndex: this.dataIndex,
      key: this.dataIndex,
      ellipsis: true,
      parentDataIndex: this.parentDataIndex,
      isMerge: this.isMerge,
      // isColColumn: this.isColColumn,
      align: 'center',
      width: 50,
      scopedSlots: {
        filterDropdown: 'merge',
        filterIcon: 'mergeIcon',
        customRender: 'cell',
      },
    }
  }

  /* private methods */
}

export default FCRuleTableColumn
export { COLUMN_TYPE }
